﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Osteox.ManagedCv;
using Osteox.ManagedCv.Wpf;

namespace WpfSample
{
    /// <summary>
    /// Interaction logic for BrowseImagesDialog.xaml
    /// </summary>
    public partial class BrowseImagesDialog : Window
    {
        public BrowseImagesDialog(IEnumerable<ManagedImage> images)
        {
            InitializeComponent();
            int index = 0;
            foreach (ManagedImage image in images)
            {
                ImageViewer viewer = new ImageViewer();
                viewer.Margin = new Thickness(2);
                viewer.Source = image;

                CheckBox checker = new CheckBox();
                checker.Content = string.Empty;

                Grid grid = new Grid();
                grid.Children.Add(viewer);
                grid.Children.Add(checker);

                Grid.SetRow(grid, index / 4);
                Grid.SetColumn(grid, index % 4);
                gridContent.Children.Add(grid);
                index++;
            }
        }

        public ICollection<ManagedImage> Selected
        {
            get
            {
                List<ManagedImage> collection = new List<ManagedImage>();
                foreach (UIElement element in gridContent.Children)
                {
                    Grid grid = element as Grid;
                    if (grid != null && grid.Children.Count == 2)
                    {
                        CheckBox checker = grid.Children[1] as CheckBox;
                        ImageViewer viewer = grid.Children[0] as ImageViewer;
                        if (checker.IsChecked == true)
                        {
                            collection.Add(viewer.Source as ManagedImage);
                        }
                    }
                }
                return collection;
            }
        }

        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void checkBoxSelectAll_Checked(object sender, RoutedEventArgs e)
        {
            foreach (UIElement element in gridContent.Children)
            {
                Grid grid = element as Grid;
                if (grid != null && grid.Children.Count == 2)
                {
                    CheckBox checker = grid.Children[1] as CheckBox;
                    checker.IsChecked = true;
                }
            }
        }

        private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (UIElement element in gridContent.Children)
            {
                Grid grid = element as Grid;
                if (grid != null && grid.Children.Count == 2)
                {
                    CheckBox checker = grid.Children[1] as CheckBox;
                    checker.IsChecked = false;
                }
            }
        }
    }
}
